<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属应用" prop="belongApplication">
        <el-input
          v-model="queryParams.belongApplication"
          placeholder="请输入归属应用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tokenList" :max-height="height">
      <el-table-column label="序号" width="70" align="center" fixed="left">
        <template scope="scope"> <span>{{scope.$index + (queryParams.pageNum - 1) * queryParams.pageSize + 1}} </span> </template>
      </el-table-column>
      <el-table-column label="token" align="center" prop="tokenValue" width="300" />
      <el-table-column label="到期时间" align="center" prop="expirationDate" width="140">
        <template slot-scope="scope">
          <span>{{ scope.row.expirationDate}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userName"  min-width="80"/>
      <el-table-column label="授权类型" align="center" prop="authorizationType" />
      <el-table-column label="归属应用" align="center" prop="belongApplication" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:token:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listToken, delToken } from "@/api/system/token";

export default {
  name: "Token",
  data() {
    return {
      height: (document.documentElement.clientHeight || document.body.clientHeight)-220,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // token管理表格数据
      tokenList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        belongApplication: null
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询token管理列表 */
    getList() {
      this.loading = true;
      listToken(this.queryParams).then(response => {
        this.tokenList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tokenIds = row.tokenId ;
      this.$confirm('是否确认删除token管理的该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delToken(row.tokenValue);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
  }
};
</script>
